Skip to content

Conversation

@vmaudgalya
Copy link

// Sample usage in webpack.config.js file
module.exports = merge(common, {
    devtool: 'eval-source-map',
    devServer: {
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,

      proxy: {
        "/first/api/*": "http://localhost:8100",
        "/second/api/*" : "http://localhost:8200",
        "/third/api/*" : "http://localhost:8300",
        "/fourth/api/*" : "http://localhost:8400",
        "/fifth/api/*" : "http://localhost:8500"
      },
      onProxyReq : function (proxyReq, req, res) {
        if (req.path.indexOf("/first/api") === 0) {
          console.log("Called first API!");
        }
        Object.keys(proxyHeaders).forEach(function (header) {
          proxyReq.setHeader(header, proxyHeaders[header]);
        });
      }
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin()
    ]
  });

Allows for listening to the onProxyReq event and performing actions accordingly (in this case setting headers)

@vmaudgalya
Copy link
Author

I am currently using the bundled files, please let me know if/when you approve of the proxy options and I'll clean up this commit and revert commit 3027379

@jamsesso
Copy link
Contributor

This ability was added in #359 (merged) along with onProxyRes and others.

@vmaudgalya
Copy link
Author

@jamsesso Thanks for letting me know! If you don't mind, could you please provide me with an example of it being used with the use case I have above (setting headers when a particular API is called)?

More specifically, how would I do this?

proxy: {
  "/first/api/*": "http://localhost:8100",
  "/second/api/*" : "http://localhost:8200",
  "/third/api/*" : "http://localhost:8300",
  "/fourth/api/*" : "http://localhost:8400",
  "/fifth/api/*" : "http://localhost:8500"
 },
 onProxyReq : function (proxyReq, req, res) {
   if (req.path.indexOf("/first/api") === 0) {
     console.log("Called first API!");
   }
   Object.keys(proxyHeaders).forEach(function (header) {
     proxyReq.setHeader(header, proxyHeaders[header]);
   });
 }

@vmaudgalya
Copy link
Author

I figured it out: http://webpack.github.io/docs/webpack-dev-server.html
If anyone finds this, I used

{
    devServer: {
        proxy: {
            '/some/path*': {
                target: 'https://other-server.example.com',
                secure: false,
                bypass: function(req, res, proxyOptions) {
                    if (req.headers.accept.indexOf('html') !== -1) {
                        console.log('Skipping proxy for browser request.');
                        return '/index.html';
                    }
                },
            },
        },
    },
}

from the documentation and modified the headers I was sending out.

@vmaudgalya vmaudgalya closed this Jan 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants